//
//  UIViewController+ZTHook.m
//  APPDemo
//
//  Created by 郭红旗 on 2021/10/14.
//

#import "UIViewController+ZTTXHook.h"
#import <objc/runtime.h>
#import "NSObject+MethodSwizzling.h"
#import "ZTTXCommonUtility.h"

static char *lbp_viewController_open_time = "lbp_viewController_open_time";
static char *lbp_viewController_close_time = "lbp_viewController_close_time";

@implementation UIViewController (ZTTXHook)

+ (void)hookUIViewController
{
    [UIViewController lbp_swizzleMethod: @selector(viewDidAppear:) swizzledSelector: @selector(hook_ViewDidAppear:)];
    [UIViewController lbp_swizzleMethod: @selector(viewWillDisappear:) swizzledSelector: @selector(hook_viewWillDisappear:)];
}
- (void)hook_ViewDidAppear:(BOOL)animated
{
    NSString *appearDetailInfo = [NSString stringWithFormat:@" %@ - %@", NSStringFromClass([self class]), @"didAppear"];
    NSLog(@"%@", appearDetailInfo);
    
    NSString *className = NSStringFromClass([self class]);
       NSString *refer = [NSString string];
       //TODO:TODO 是否只埋本地有url的page
       if ([self getPageUrl:className]) {
           //设置打开时间
          [self setOpenTime:[NSDate dateWithTimeIntervalSinceNow:0]];
           if (self.navigationController) {
               if (self.navigationController.viewControllers.count >=2) {
                   //获取当前vc 栈中 上一个VC
                   UIViewController *referVC =  self.navigationController.viewControllers[self.navigationController.viewControllers.count-2];
                   refer = [self getPageUrl:NSStringFromClass([referVC class])];
               }
           }
           if (!refer || refer.length == 0) {
               refer = @"unknown";
           }
           [self setOpenTime:[NSDate dateWithTimeIntervalSinceNow:0]];
//           [UserTrackDataCenter openPage:[self getPageUrl:className] fromPage:refer];
       }
    
    [self hook_ViewDidAppear:animated];
}

- (void)hook_viewWillDisappear:(BOOL)animated {
    NSString *className = NSStringFromClass([self class]);
    if ([self getPageUrl:className]) {
        [self setCloseTime:[NSDate dateWithTimeIntervalSinceNow:0]];
//        [UserTrackDataCenter leavePage:[self getPageUrl:className] spendTime:[self p_calculationTimeSpend]];
    }
    [self hook_viewWillDisappear:animated];
}



#pragma mark - private method

- (NSString *)getPageUrl:(NSString *)className{
    return  @"";
}
 
- (NSString *)p_calculationTimeSpend {
    
    if (![self getOpenTime] || ![self getCloseTime]) {
        return @"unknown";
    }
    NSTimeInterval aTimer = [[self getCloseTime] timeIntervalSinceDate:[self getOpenTime]];
    
    int hour = (int)(aTimer/3600);
    
    int minute = (int)(aTimer - hour*3600)/60;
    
    int second = aTimer - hour*3600 - minute*60;
    
    return [NSString stringWithFormat:@"%d",second];
}

#pragma mark - add prop
 
- (void)setOpenTime:(NSDate *)openTime {
    objc_setAssociatedObject(self,&lbp_viewController_open_time, openTime, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (NSDate *)getOpenTime{
    return objc_getAssociatedObject(self, &lbp_viewController_open_time);
}
 
- (void)setCloseTime:(NSDate *)closeTime {
    objc_setAssociatedObject(self,&lbp_viewController_close_time, closeTime, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (NSDate *)getCloseTime{
    return objc_getAssociatedObject(self, &lbp_viewController_close_time);
}

#pragma mark - properties

- (BOOL)sensorsdata_isIgnored {
    return YES;
}

- (NSString *)sensorsdata_screenName {
    return NSStringFromClass([self class]);
}

- (NSString *)sensorsdata_title {
    __block NSString *titleViewContent = nil;
    __block NSString *controllerTitle = nil;
    [ZTTXCommonUtility performBlockOnMainThread:^{
        controllerTitle = self.navigationItem.title;
    }];
    if (titleViewContent.length > 0) {
        return titleViewContent;
    }

    if (controllerTitle.length > 0) {
        return controllerTitle;
    }
    return nil;
}

- (NSString *)sensorsdata_heatMapPath {
    /* 忽略路径
     UITableViewWrapperView 为 iOS11 以下 UITableView 与 cell 之间的 view
     */
    return  NSStringFromClass(self.class);
}

@end
